﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QThread>
#include <QMessageBox>
#include <QDateTime>
#include <QVector>
#include "modbusRtu.h"
#include "ui_serialSet.h"

#define g_SoftwareVersion "1.3.3"

#define FUN_PAGE_NUM 5

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

signals:
    void searchSerialPortrt(void);               // 查询可用串口
    void setMosbusParam(ModbusRtuParam_t param); // 设置Modbus参数
    void setModbusEnable(bool en, QString port, QString baud); // 设置Mosbus状态
    void modbusSetRegs(ModbusRegCmd_e type, QModbusDataUnit writeData);


private slots:
    void modbusWriteFeedback(ModbusRegCmd_e type, bool ret);

    void on_setMaddrBtn_clicked();
    void on_getSysTimeBtn_clicked();
    void on_setSysTimeBtn_clicked();
    void on_rebootBtn_clicked();
    void on_resetBtn_clicked();
    void on_localSetMaddrBtn_clicked();
    void on_localSetBaudrateBtn_clicked();
    void on_localSetLogLevelBtn_clicked();
    void on_btnMotorAlgo1_clicked();
    void on_btnMotorAlgo2_clicked();
    void on_btnMotorStop1_clicked();
    void on_btnMotorStop2_clicked();
    void on_btnMotorReset1_clicked();
    void on_btnMotorReset2_clicked();
    void on_btnMotorZero1_clicked();
    void on_btnMotorZero2_clicked();
    void on_btnMotorPos1_clicked();
    void on_btnMotorPos2_clicked();
    void on_btnMotorForward1_clicked();
    void on_btnMotorReverse1_clicked();
    void on_btnMotorForward2_clicked();
    void on_btnMotorReverse2_clicked();
    void on_btnMotorResetPos1_clicked();
    void on_btnMotorResetPos2_clicked();
    void on_btnMotorPowerMax1_clicked();
    void on_btnMotorPowerMax2_clicked();
    void on_localSetClusterSwitchBtn_clicked();
    void on_localSetMotorSwitchBtn_clicked();
    void on_btnMotorSetLog1_clicked();
    void on_btnMotorSetLog2_clicked();
    void on_locErrCodeResetBtn_clicked();
    void on_locEmergencyResetBtn_clicked();
    void on_locEmerStopBtn_clicked();

    void on_auto_switch_enable_clicked();
    void on_engineer_authority_clicked();
    void on_simulate_reflected_to_safety_zone_clicked();
    void on_in_calibration_queue_clicked();
    void on_disable_backup_point_corrdinate_limit_clicked();
    void on_simulate_zero_point_correction_ready_clicked();
    void on_simulate_check_limit_pos_ready_clicked();
    void on_simulate_back_horizontal_ready_clicked();
    void on_simulate_emergency_placement_clicked();
    void on_code_multiplier_val_enable_clicked();
    void on_enable_incident_light_dead_zone_clicked();
    void on_disable_backup_point_distance_requirements_clicked();
    void on_enable_advance_travel_clicked();
    void on_angle_travel_advance_1min_clicked();
    void on_enable_gravity_compensation_clicked();
    void on_enable_correction_compensation_clicked();
    void on_locPowerLimitSetBtn_clicked();
    void on_btnStop_clicked();
    void on_btnSetCmd_clicked();
    void on_btnSetDna_clicked();
    void on_localSetPosBtn_clicked();

    void on_localDnaSaveBtn_clicked();

private:
    Ui::MainWindow *ui;

    /* 寄存器信息 */
    localInfo_s * m_info;

    /* 功能页面 */
    QPushButton * m_funPageBtn[FUN_PAGE_NUM];

    /* HMI */
    QPushButton * m_hmiBusyBtn[2];
    QPushButton * m_hmiOnlineBtn[2];
    QPushButton * m_hmiModeBtn[5];

    /* 串口 */
    QThread * m_modbusThread = nullptr;
    Modbus * m_modbus = nullptr;

    /* 定时器 */
    QTimer * m_timer1s = nullptr;

    void refrSerialPort(QStringList portList); // 刷新可用串口
    void modbusDataReady(ModbusRegCmd_e type, QModbusDataUnit data); // modbus接收数据

    void modbusGetSystemHandle(QVector<quint16> data);    // 系统寄存器处理
    void modbusGetMotorCtrlHandle(QVector<quint16>data , int motor); // 电机控制状态寄存器处理
    void modbusGetAppHandle(QVector<quint16> data);        // 应用寄存器处理
    void modbusGetMotorDataHandle(QVector<quint16> data);  // 电机实时数据寄存器处理
    void modbusGetAlogDataHandle(QVector<quint16> data);   // 算法实时数据寄存器处理
    void modbusGetSystemTimeHandle(QVector<quint16> data); // 获取系统时间处理
    void modbusGetCmdHandle(QVector<quint16> data); // 获取CMD数据处理
    void modbusGetDnaHandle(QVector<quint16> data); // 获取DNA数据处理


    void modbusRegContral(quint16 regAddr, quint16 data = 1, ModbusRegCmd_e type = MODBUS_REG_FEEDBACK);
    void modbusRegContral(quint16 regAddr, float data, ModbusRegCmd_e type = MODBUS_REG_NO_FEEDBACK);
    void modbusRegContral(quint16 regAddr, int data, ModbusRegCmd_e type = MODBUS_REG_NO_FEEDBACK);
    QModbusDataUnit modbusMotorContralDefault(int motorType);
    QModbusDataUnit makeMotorContralBuffer(int starRegAddr);

    void uint16ToDoubleByte(float &src, quint16 * u);
    void uint16ToDoubleByte(int &src, quint16 * u);

    void unpackVersion(QVector<quint16> data, char * version);

};
#endif // MAINWINDOW_H
